package com.seo.controller.user.proxy;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.InputBody;
import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.proxy.menu.sdk.ProxyUserMenuService;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * menucontroller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/user/proxy/menu")
public class ProxyUserMenuController {

	@Autowired
	private ProxyUserMenuService proxyUserMenuService;

	private String menuUrl = "user/proxy/menu";

	@RequestMapping
	public String list(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("进入菜单...");
		if (!Jurisdiction.qx(menuUrl)) {
			throw new DefiniteException("权限不足");
		}

		if (parentId == null) {
			parentId = 0;
		}
		if (parentId != 0) {
			Map<String, Object> pMenu = proxyUserMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}
		List<Map<String, Object>> menuList = proxyUserMenuService.getMenuByParentId(parentId);
		request.setAttribute("parentId", parentId);
		request.setAttribute("menuList", menuList);
		return "user/proxy/menu/list";

	}

	@RequestMapping("/getList")
	@ResponseBody
	public ReturnBody getList(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("获取菜单...");
		ReturnBody body = ReturnBody.init();
		if (!Jurisdiction.qx(menuUrl)) {
			return body.error("error", "权限不足");
		}

		if (parentId == null) {
			parentId = 0;
		}
		List<Map<String, Object>> menuList = proxyUserMenuService.getMenuByParentId(parentId);
		body.put("menuList", menuList);
		return body.success();

	}

	@RequestMapping("/add")
	public String add(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("去添加菜单页...");
		if (!Jurisdiction.qx(menuUrl, "add")) {
			throw new DefiniteException("权限不足");
		}
		if (parentId == null) {
			parentId = 0;
		}
		if (parentId != 0) {
			Map<String, Object> pMenu = proxyUserMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}
		request.setAttribute("action", "saveMenu");
		request.setAttribute("parentId", parentId);
		return "user/proxy/menu/edit";

	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request, HttpSession session) {
		log.info("去编辑菜单页...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		if (id == null) {
			throw new DefiniteException("参数不能为空");
		}
		Map<String, Object> menu = proxyUserMenuService.getMenuById(id);
		if (menu == null) {
			throw new DefiniteException("菜单不存在");
		}
		Integer parentId = (Integer) menu.get("parentId");
		if (parentId != 0) {
			Map<String, Object> pMenu = proxyUserMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}

		request.setAttribute("action", "editMenu");
		request.setAttribute("menu", menu);
		request.setAttribute("parentId", parentId);
		return "user/proxy/menu/edit";

	}

	@RequestMapping("/editMenu")
	public String editMenu() {
		log.info("保存菜单...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		InputBody body = InputBody.init();
		log.info("参数-{}", body.getParams());
		ReturnBody rb = proxyUserMenuService.updateMenu(body);
		if (!rb.isSuccess()) {
			throw new DefiniteException("保存失败");
		}
		String parentId = body.get("parentId", String.class);
		return "redirect:/user/proxy/menu?parentId=" + parentId;

	}

	@RequestMapping("/saveMenu")
	public String saveMenu() {
		log.info("保存菜单...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		InputBody body = InputBody.init();
		log.info("参数-{}", body.getParams());
		ReturnBody rb = proxyUserMenuService.saveMenu(body);
		if (!rb.isSuccess()) {
			throw new DefiniteException("保存失败");
		}
		String parentId = body.get("parentId", String.class);
		return "redirect:/user/proxy/menu?parentId=" + parentId;

	}

	@RequestMapping("/deleteMenu")
	@ResponseBody
	public ReturnBody deleteMenu(Integer id) {
		log.info("删除菜单...");
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			return rb.error("error", "权限不足");
		}
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}
		return proxyUserMenuService.deleteMenu(id);
	}

}
